import matplotlib.pyplot as plt
import numpy as np

import os
path = os.path.dirname(__file__) # 先找到当前文件 a.py 所在的目录
file_name1 = os.path.join(path,'D_error1.txt') # 拼接文件的路径
file_name2 = os.path.join(path,'D_error3.txt')

a = []
b = []

file1 = open(file_name1, mode = 'r')
for line in file1:
    line = line.split()
    a.append(line)
file1.close()

file2 = open(file_name2, mode = 'r')
for line in file2:
    line = line.split()
    b.append(line)
file2.close()

a1 = []
for m in a:
    a1.append(float(m[0]))
a = a1
b1 = []
for m in b:
    b1.append(float(m[0]))
b = b1
n1 = 900
n3 = 1000
x1 = np.linspace(-5,5,n1)
x3 = np.linspace(-5,5,n3)
#plt.figure(1)
Es1=np.zeros((n1,1))
Es3=np.zeros((n3,1))
for i in range(0,n1):
    Es1[i][0] = a[i]
for i in range(0,n3):
    Es3[i][0] = b[i]
plt.plot(x1,Es1[:,0])
plt.plot(x3,Es3[:,0])
plt.legend(['order=1','order=3'])
plt.savefig(os.path.join(path,"figure/D_error.png"))